package com.example.demo1.service.impl;

import com.example.demo1.mapper.ArticleMapper;
import com.example.demo1.pojo.Article;
import com.example.demo1.pojo.PageBean;
import com.example.demo1.service.ArticleService;
import com.example.demo1.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public Integer add(Article article) {

        LocalDateTime dateTime = LocalDateTime.now();
        String date= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(dateTime);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parsedDate = LocalDateTime.parse(date, formatter);

        article.setCreateTime(parsedDate);
        article.setUpdateTime(parsedDate);
        Map<String,Object> user = ThreadLocalUtil.get();
        Integer id = (Integer) user.get("id");
        article.setCreateUser(id);
        articleMapper.add(article);

        Integer articleid = articleMapper.idSelect(date);
        System.out.println(articleid);
        return articleid;
    }

//    @Override
//    public PageBean<Article> list(Integer pageNum, Integer pageSize, Integer categoryId, String state) {
//        //创建PageBean对象
//        PageBean<Article> pb = new PageBean<>();
//        //开启分页查询PageHelper
//        PageHelper.startPage(pageNum,pageSize);
//        Map<String,Object> user = ThreadLocalUtil.get();
//        Integer id = (Integer) user.get("id");
//        List<Article> as = articleMapper.list(id,categoryId,state);
//        //强转可以调用page提供的方法
//        Page<Article> p = (Page<Article>) as;
//        //将数据填充到pb中
//        pb.setTotal(p.getTotal());
//        pb.setItems(p.getResult());
//        return pb;
//    }
    @Override
    public PageBean<Article> list(Integer pageNum, Integer pageSize, Integer categoryId, String state, String content) {
        //创建PageBean对象
        PageBean<Article> pb = new PageBean<>();
        //开启分页查询PageHelper
        PageHelper.startPage(pageNum,pageSize);
        Map<String,Object> user = ThreadLocalUtil.get();
        Integer id = (Integer) user.get("id");
        List<Article> as = articleMapper.list(id,categoryId,state,content);
        System.out.println(content);
        //强转可以调用page提供的方法
        Page<Article> p = (Page<Article>) as;
        //将数据填充到pb中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public String upload(MultipartFile file, Integer id) {
        FileOutputStream fos = null;
        InputStream fis = null;
        byte[] bytes = new byte[20];

        System.out.println("添加图片 " + id + " " + file.getName());
        String filePath=null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd_HH.mm.ss");
        Date date = new Date();
        String currentTime = sdf.format(date);
//        Integer uid = 0;
//        if(id==null) {
//            articleMapper.addimg(currentTime+files);
//            uid = articleMapper.idSelect(currentTime+files);
//            System.out.println(uid);
//            id = uid;
//        }
//        System.out.println(id+" "+uid);
        try {
            fos = new FileOutputStream("C:\\Users\\jizhaoyang\\Documents\\idea_project\\webproject\\demo1\\src\\main\\resources\\static\\coverImg\\"  + currentTime + "_Message_" + id + ".jpg");

            fis = file.getInputStream();
            int read = fis.read(bytes);
            while (read != -1) {
                fos.write(bytes, 0, bytes.length);
                read = fis.read(bytes);
            }
            filePath = articleMapper.uploadSelect(id);
            if(filePath!=null){
                File deleteFile = new File(filePath);
                String fileName = deleteFile.getName();
                deleteFile = new File("C:\\Users\\jizhaoyang\\Documents\\idea_project\\webproject\\demo1\\src\\main\\resources\\static\\coverImg\\" + fileName);
                if(deleteFile.exists() && !fileName.equals("default.jpeg")){
                    deleteFile.delete();
                    System.out.println("文件已成功删除");
                }
            }
            filePath = "http://localhost:8080/coverImg/" + currentTime + "_Message_" + id + ".jpg";
            System.out.println(filePath+"  "+id);
            articleMapper.upload(filePath, id);
        } catch (Exception e) {
            System.out.println(e);
            return "500";
        } finally {
            try {
                fos.close();
                fis.close();
            } catch (Exception e) {
                System.out.println(e);
                return "500";
            }
        }
        return filePath;
    }

    @Override
    public Integer update(Article article) {
        LocalDateTime dateTime = LocalDateTime.now();
        String date= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(dateTime);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parsedDate = LocalDateTime.parse(date, formatter);

        article.setUpdateTime(parsedDate);
        Map<String,Object> user = ThreadLocalUtil.get();
        Integer id = (Integer) user.get("id");
        article.setCreateUser(id);
        articleMapper.update(article);

        Integer articleid = article.getId();
        System.out.println(articleid+" "+article);
        return articleid;
    }

    @Override
    public void deleteById(Integer id) {
        articleMapper.deleteById(id);
    }


}
